# -*- coding: utf-8 -*-
from nltk.corpus import *
from lex2toolbox import *
from myToolbox import *
from config import *

separator = '-'*80

##### CONVERSION #######################################################################

#Converting Lexschem
Source = relpath('/lexicons/lexschem/lexschem.scf')
Target = relpath('/lexiconsTB/lexschem.txt')
print 'Converting %s to Toolbox format ...' %Source
lexschem = LexschemConverter()
lexschem.convert2TB(Source, Target)

#Converting Treelex
Source = relpath('/lexicons/treelex/treelex.txt')
Target = relpath('/lexiconsTB/treelex.txt')
print separator+'\nConverting %s to Toolbox format ...' %Source
treelex = TreelexConverter()
treelex.convert2TB(Source, Target)

#Converting Synlex
print separator+'\nConverting %s to Toolbox format ...' %Source
Source = relpath('/lexicons/synlex/synlex.txt')
Target = relpath('/lexiconsTB/synlex.txt')
synlex = SynlexConverter()
synlex.convert2TB(Source, Target)


##### TOOLBOX ##########################################################################

#Creating a corpus reader object
reader  = LazyCorpusLoader('toolbox', MyExtendedToolboxCorpusReader, MAINPATH, ENCODING)
       
#save lexicon as xml, html file
Source = relpath('/lexiconsTB/synlex.txt')
print separator+'\nSaving %s as xml and html file...' % Source
Lexicon = reader.xml(Source)

xmlfile = relpath('/output/synlex.xml')
reader.output(Lexicon,'xml',xmlfile)

htmlfile = relpath('/output/synlex.html')
reader.output(Lexicon,'html',htmlfile)

#Check lexicon for errors
Source = relpath('/lexiconsTB/synlex.txt')
Error = relpath('/output/synlex-error.html')
print separator+'\nChecking error in %s ...' % Source
Lexicon = reader.xml(Source)
ErrorLexicon = reader.errorCheck(Lexicon)
reader.output(ErrorLexicon, 'html', Error)
print '%s errors found' % reader.numEntries(ErrorLexicon)

#Fusion 
Source1 = relpath('/lexiconsTB/synlex.txt')
Source2 = relpath('/lexiconsTB/treelex.txt')
Target = relpath('/output/synlex_treelex.html')
print separator+"\nApply fusion on %s %s ..." %(Source1, Source2)
Lexicon1 = reader.xml(Source1)
Lexicon2 = reader.xml(Source2)
FusedLexicon = reader.fuse([Lexicon1, Lexicon2])
reader.output(FusedLexicon,'html',Target)
print 'Lexicon1 with %s entries combines with Lexicon2 with %s entries and fused lexicon has %s entries' % (reader.numEntries(Lexicon1),reader.numEntries(Lexicon2), reader.numEntries(FusedLexicon))

#Extraction
Source = relpath('/lexiconsTB/synlex.txt')
Target = relpath('/output/synlex_a.html')
VerbRegex = 'a.*'
print separator+'\nExtracting %s from %s ...' % (VerbRegex, Source)
Lexicon = reader.xml(Source)
NewLexicon = reader.filter(Lexicon, VerbRegex, 0)
reader.output(NewLexicon,'html',Target)
print 'Extract %s entries from lexicon with %s entries' %  (reader.numEntries(NewLexicon),  reader.numEntries(Lexicon)) 


##### ANALYSIS #########################################################################
Source=relpath('/lexiconsTB/synlex.txt')
Lexicon=reader.xml(Source)

#Analysis
print separator+'\nAnalyzing %s...' % Source
NumEntries = reader.numEntries(Lexicon)
NumVerbs = reader.numVerbs(Lexicon)
NumScfs = reader.numScfs(Lexicon)
ratioVerbsScf = round(reader.ratioVerbsScf(Lexicon),2)
ratioScfsVerb = round(reader.ratioScfsVerb(Lexicon),2)
print '\nOUTPUT ANALYSIS'
print 'Num of entries:         %s' % NumEntries
print 'Num of verbs:           %s' % NumVerbs
print 'Num of subcat frames:   %s' % NumScfs
print 'Avg num of verbs/frame: %s' %ratioVerbsScf
print 'Avg num of frames/verb: %s' %ratioScfsVerb

#Verb search - for all scfs with given pattern
ScfRegex =  'SUJ:NP,OBJ:NP'
print separator+'\nSearching for verb with %s scfs ...' % ScfRegex
Verbs = reader.verbs(Lexicon, ScfRegex)
print 'Num of verbs found is %s' % len(Verbs)

#Scf search - for all verb begin with a
VerbRegex = 'a.*'
print separator+'\nSearching for scf with %s verbs ...' %VerbRegex
scfs = reader.scfs(Lexicon, VerbRegex)
print 'Num of scfs found is %s' % len(scfs)

print "------------------------------- finished. --------------------------------------"
